/*
 * @(#) $Header$
 *
 * Copyright (C)  2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util;

import java.lang.reflect.Method;
import ca.forklabs.baselib.util.function.BinaryCompose;
import ca.forklabs.baselib.util.function.Bind1stFunction;
import ca.forklabs.baselib.util.function.Bind2ndFunction;
import ca.forklabs.baselib.util.function.IdentityFunction;
import ca.forklabs.baselib.util.function.Project1stFunction;
import ca.forklabs.baselib.util.function.Project2ndFunction;
import ca.forklabs.baselib.util.function.UnaryClassFunction;
import ca.forklabs.baselib.util.function.UnaryCompose;

/**
 * Class {@code Functions} provides factory methods to create functions.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.Functions">Daniel L�onard</a>
 * @version $Revision$
 */
public class Functions {

//---------------------------
// Constructor
//---------------------------

   /**
    * Let no one instanciate this class.
    */
   protected Functions() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Makes <em>f(g(x))</em>.
    * @param   <R>   the type of the returned object of <em>f(y)</em>.
    * @param   <I>   the type of the argument object of <em>f(y)</em>, and the
    *                type of the returned object of <em>g(x)</em>.
    * @param   <A>   the type of the argument object of <em>g(x)</em>.
    * @param   f   <em>f(y)</em>.
    * @param   g   <em>g(x)</em>.
    * @return   <em>f(g(x))</em>.
    * @see   UnaryCompose
    */
   public static <R, I, A> UnaryFunction<R, A> compose1(UnaryFunction<R, I> f, UnaryFunction<I, A> g) {
      UnaryFunction<R, A> function = new UnaryCompose<>(f, g);
      return function;
      }

   /**
    * Makes <em>f(g1(x), g2(x))</em>.
    * @param   <R>   the type of the returned object of <em>f(y, z)</em>.
    * @param   <I1>   the type of the first argument object of
    *                 <em>f(y, z)</em>, and the type of the returned object of
    *                 <em>g1(x)</em>.
    * @param   <I2>   the type of the second argument object of
    *                 <em>f(y, z)</em>, and the type of the returned object of
    *                 <em>g2(x)</em>.
    * @param   <A>   the type of the argument object of <em>g1(x)</em> and
    *                <em>g2(x)</em>.
    * @param   f   <em>f(y, z)</em>.
    * @param   g1   <em>g1(x)</em>.
    * @param   g2   <em>g2(x)</em>.
    * @return   <em>f(g1(x), g2(x))</em>.
    * @see   UnaryCompose
    */
   public static <R, I1, I2, A> UnaryFunction<R, A> compose2(BinaryFunction<R, I1, I2> f, UnaryFunction<I1, A> g1, UnaryFunction<I2, A> g2) {
      UnaryFunction<R, A> function = new BinaryCompose<>(f, g1, g2);
      return function;
      }

   /**
    * Gives the identity function.
    * @param   <A>   the type of the argument.
    * @return   the identity function.
    */
   public static <A> UnaryFunction<A, A> identity() {
      UnaryFunction<A, A> identity = new IdentityFunction<>();
      return identity;
      }

   /**
    * Gives the project 1st function.
    * @param   <A1>   the type of the first argument object, and the type of the
    *                 returned object.
    * @param   <A2>   the type of the second argument object.
    * @return   the project 1st function.
    */
   public static <A1, A2> BinaryFunction<A1, A1, A2> project1st() {
      BinaryFunction<A1, A1, A2> project_1st = new Project1stFunction<>();
      return project_1st;
      }

   /**
    * Gives the project 2nd function.
    * @param   <A1>   the type of the first argument object.
    * @param   <A2>   the type of the second argument object, and the type of the
    *                 returned object.
    * @return   the project 2nd function.
    */
   public static <A1, A2> BinaryFunction<A2, A1, A2> project2nd() {
      BinaryFunction<A2, A1, A2> project_2nd = new Project2ndFunction<>();
      return project_2nd;
      }

   /**
    * Makes <em>f(c, x)</em>.
    * @param   <R>   the type of the returned object.
    * @param   <A1>   the type of the first argument object.
    * @param   <A2>   the type of the second argument object.
    * @param   f   the binary function.
    * @param   arg1   the binded first argument.
    * @return   the bind1st function.
    */
   public static <R, A1, A2> UnaryFunction<R, A2> bind1st(BinaryFunction<R, A1, A2> f, A1 arg1) {
      UnaryFunction<R, A2> bind1st = new Bind1stFunction<>(f, arg1);
      return bind1st;
      }

   /**
    * Makes <em>f(x, c)</em>.
    * @param   <R>   the type of the returned object.
    * @param   <A1>   the type of the first argument object.
    * @param   <A2>   the type of the second argument object.
    * @param   f   the binary function.
    * @param   arg2   the binded second argument.
    * @return   the bind2nd function.
    */
   public static <R, A1, A2> UnaryFunction<R, A1> bind2nd(BinaryFunction<R, A1, A2> f, A2 arg2) {
      UnaryFunction<R, A1> bind2nd = new Bind2ndFunction<>(f, arg2);
      return bind2nd;
      }

   /**
    * Creates a new unary class function.
    * @param   method   the static method.
    * @exception   NullPointerException   if the method is {@code null}.
    * @exception   IllegalArgumentException   if the method is not static.
    * @return   a new unary class function.
    */
   public static <R, A> UnaryFunction<R, A> unaryClassFunction(Method method) {
      UnaryClassFunction<R, A> function = UnaryClassFunction.create(method);
      return function;
      }

   /**
    * Creates a new unary class function.
    * @param   clazz   the {@link Class} where the method is located.
    * @param   name   the name of the static method.
    * @param   param   the {@link Class} of the argument.
    * @exception   NullPointerException   if {@code clazz} is {@code null}.
    * @exception   NoSuchMethodException   if no method is found.
    * @exception   IllegalArgumentException   if the method is not static.
    * @return   a new unary class function.
    */
   public static <R, A> UnaryFunction<R, A> unaryClassFunction(Class<?> clazz, String name, Class<?> param) throws NoSuchMethodException {
      UnaryClassFunction<R, A> function = UnaryClassFunction.create(clazz, name, param);
      return function;
      }

   }
